package top.healthylife.controller;/*
 *@Author GaoZeXi
 *@Created time 2020/4/2 1:28
 *@Description:
 * Step by Step  and Stand on,  You Are The Best Investment!!!
 */

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.healthylife.constant.MessageConstant;
import top.healthylife.entity.Result;

@RestController
@RequestMapping("/user")
public class UserController {
    //使用dubbo注册服务时,不检查provider和consumer的启动顺序
    //使用zookeeper的负载均衡方式 为随机

    @RequestMapping("/getUsername")
    public Result getUsername() throws Exception{
        try {
            User userDetails =(User)
                    SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,
                    userDetails.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_USERNAME_FAIL);
        }

    }
}
